<?php

namespace app\job;
use think\queue\Job;
use think\Log;
use think\Db;
class OrderJob{
    
    public function fire(Job $job, $data)
    {
        // 有些消息在到达消费者时,可能已经不再需要执行了
        $isJobStillNeedToBeDone = $this->checkDatabaseToSeeIfJobNeedToBeDone($data);
        if(!$isJobStillNeedToBeDone){
            $job->delete();
            return;
        }
        
        $isJobDone = $this->doJob($data);
        
        if ($isJobDone) {
            // 如果任务执行成功， 记得删除任务
            $job->delete();
            print("<info>Hello Job has been done and deleted"."</info>\n");
        }else{
            if ($job->attempts() > 3) {
                //通过这个方法可以检查这个任务已经重试了几次了
                print("<warn>Hello Job has been retried more than 3 times!"."</warn>\n");
                
                $job->delete();
                
                // 也可以重新发布这个任务
                //print("<info>Hello Job will be availabe again after 2s."."</info>\n");
                //$job->release(2); //$delay为延迟时间，表示该任务延迟2秒后再执行
            }
        }
    
    }
    
    /**
     * 有些消息在到达消费者时,可能已经不再需要执行了
     * @param array|mixed    $data     发布任务时自定义的数据
     * @return boolean                 任务执行的结果
     */
    private function checkDatabaseToSeeIfJobNeedToBeDone($data){
        return true;
    }
    
    /**
     * 根据消息中的数据进行实际的业务处理...
     */
    private function doJob($data)
    {
      
        Db::name("test")->insert([
            "text"=>'balance',
            "create_time"=>date("Y-m-d H:i:s"),
        ]);
        $card_id=$data['card_id'];
        $order_id=$data['order_id'];
        // 查询该卡的余额
        $card=Db::name("card")->find($card_id);
        $header[]=$card['user_token'];
        $header[]="content-type:application/json";
        $url="https://b2bvcard.shell.com.cn/bhz-vcs-service/apis/v1/availablebalance?pan_code=".$card['pan_code']."&user_id=".$card['you_user_id']."&cellphone=".$card['cellphone']."&org_id=".$card['org_id'];
        
        $info = $this->getUrl($url,$header);
        $available_balance=$info['available_balance'];
        $balance=$card['balance'];
        //更新卡余额
        Db::name("card")->where("id",$card_id)->update([
            "balance"=>$available_balance,
            "balance_time"=>date("Y-m-d H:i:s"),
        ]);
        
        Db::name("card_log")
        ->where("order_id",$order_id)
        ->where("card_id",$card_id)
        ->update([
            "money"=>$balance-$available_balance,
            "update_time"=>date("Y-m-d H:i:s"),
        ]);
        
        return true;
    }
    
    protected function geturl($url,$headerArray){
        //$headerArray =array("Content-type:application/json;","Accept:application/json");
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray);
        $output = curl_exec($ch);
        curl_close($ch);
        $output = json_decode($output,true);
        return $output;
    }
    
    
    public function balance(Job $job, $data)
    {
        Db::name("test")->insert([
            "text"=>'bbs',
            "create_time"=>date("Y-m-d H:i:s"),
        ]);
        return true;
    }
    
    public function failed($data){
        Log::write('任务达到最大重试次数后:'.print_r($data,1),'job');
        // ...任务达到最大重试次数后，失败了
    }
}